
* Panel A: Main Outcomes (ITT)
	eststo clear
	foreach x of numlist 1/3 {
		use "$data\4_individual_ano_reg_`x'.dta", clear
		ren assignment ITT
		local c=1 
		foreach o of global outcomes {
			local d=`c'+1
			eststo m`c'`x': areg `o' ITT `o'_2021 missing_`o'_2021 i.wave $se_indiv
			qui summ `o' if ITT==0
			local mu : di %5.2f r(mean)
			estadd local mu `mu'
			local ++c 
		}
	}

	esttab m1* m2* m3* m4*,    keep(ITT) ///
    b(%5.3f) se(%5.3f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
	nonumbers ///
    scalars("r2 R2" "mu Mean") mtitle("6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled") mgroup("Min. Wage" "Wrcon" "Sosec" "Formality", pattern(1 0 0 1 0 0  1 0 0 1 0 0))
	
	
* Create Panel A table ITT
esttab m1* m2* m3* m4* using "$results\01_tables\Table_S1_robustness_nocontrols.tex", ///
   keep(ITT) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(mu N, fmt(%9.2f %9.0f) labels("Mean" "\(N\)")) ///		
    nomtitles ///
    noline  ///
	nonotes ///
	nonumbers /// 
	prehead("\begin{tabular}{l*{12}{c}}\hline\hline\\[2pt]\multicolumn{13}{l}{\textbf{Panel A: Main Outcomes }}\\[2pt]\hline\\[2pt]&\multicolumn{3}{c}{Min. Wage (0/1)}&\multicolumn{3}{c}{Written Contract (0/1)}&\multicolumn{3}{c}{Social Security (0/1)}&\multicolumn{3}{c}{Formality Index (0-1)}\\\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}\cmidrule(lr){11-13}\\[2pt]") ///
		posthead("&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}\\[2pt]\hline\\") ///
		prefoot("") ///
		postfoot("\\") ///
		replace
		
	
	
*Panel B: Additional Outcomes (ITT)
eststo clear
	foreach x of numlist 1/3 {
		use "$data\4_individual_ano_reg_`x'.dta", clear
		merge m:1 id wave using "$data\2_firm_regressions_individual_paper.dta", keepusing(empcat costs_labour)
		ren assignment ITT
		local c=1 
		foreach o of global outcomes2 {
			local d=`c'+1
			eststo a`c'`x': areg `o' ITT `o'_2021 missing_`o'_2021 i.wave $se_indiv 
			qui summ `o' if ITT==0
			local mu : di %5.2f r(mean)
			estadd local mu `mu'
			local ++c 
		}
	}

	
	esttab a1* a2* a3* a4*,    keep(ITT) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
	nonumbers ///
    scalars("r2 R2" "mu Mean") mtitle("6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled" "6 M" "18 M" "Pooled") mgroup("Log. Wage" "Satifaction" "Hours Worked" "Training", pattern(1 0 0 1 0 0  1 0 0 1 0 0))
	
	
*Create Panel B ITT
esttab a1* a2* a3* a4* using "$results\01_tables\Table_S1_robustness_nocontrols.tex", ///
   keep(ITT) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(mu N, fmt(%9.2f %9.0f) labels("Mean" "\(N\)")) ///		
    nomtitles ///
    noline  ///
	nonotes ///
	nonumbers /// 
	prehead("\multicolumn{13}{l}{\textbf{Panel B: Additional Outcomes}}\\\hline\\[2pt]&\multicolumn{3}{c}{Log. Wage)}&\multicolumn{3}{c}{Satisfaction (0/1)}&\multicolumn{3}{c}{Hours worked (0/1)}&\multicolumn{3}{c}{Training (0/1)}\\\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}\cmidrule(lr){11-13}\\[2pt]") ///
		posthead("&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{Pooled}\\[2pt]\hline\\") ///
		prefoot("") ///
		postfoot("\\") ///
		append
		
	
*Panel C: Retention
		
* Panel D: Retention Analysis

eststo clear

* Load individual-level data
use 	"$data\4_individual_ano_vars.dta", clear

by 		id_indiv: egen number = count(id_indiv)

* Merge retention info from firm-level data
merge 	m:1 id wave using "$data\2_firm_regressions_individual_paper.dta", keepusing(empretention_2021 morethan1visit)
ren 	empretention_2021 empretention_raw_2021
replace empretention_raw_2021 = 0 if missing(empretention_raw_2021)
gen 	left_firm_raw_2021 = 1 - empretention_raw_2021
drop 	_merge

* Merge additional data for balancing
merge m:1 id_indiv using "$data\4_data_for_balance.dta", ///
	keepusing(left_firm_22 used_w2_w1 firm_att left_firm_23 left_firm22 left_firm23 used_w3_w1 used_w2_w1)

* Standardize left firm variable
egen 	left_firm_2021 = std(left_firm_raw_2021)
replace left_firm_2021 = 0 if missing(left_firm_raw_2021)
gen 	missing_left_firm_2021 = missing(left_firm_raw_2021)

* Treatment assignment
ren 	assignment ITT

* ----------------------------------------------
* Construct analysis sample 
* ----------------------------------------------
gen 			emp_w1_temp = 1 if employer==1 & wave ==1
bysort 			id_indiv: egen emp_w1 = max(emp_w1_temp)

gen 			int_w1t 	= 1 if wave == 1
bys				id_indiv: 	egen int_w1 = max(int_w1t)
drop 			int_w1t
sort 			id_indiv wave
replace 		employer = employer[_n-1] if wave == 2 & status == 3 & id_indiv == id_indiv[_n-1] & int_w1 == 1
replace 		employer = employer[_n-1] if wave == 3 & status == 3 & id_indiv == id_indiv[_n-1] 

*Wave 2
gen 	left_firm_w2 = 1 if left_firm22 == 1 & wave == 1
replace left_firm_w2 = 0 if used_w2_w1 == 1 & wave == 1
bys 	id_indiv: egen temp = max(left_firm_w2)

replace left_firm_w2 = 0 if wave == 2 & status == 1 & employer == 0 & consent_individual == 1 & missing(temp)

gen 	temp2 = 1 if used_w3_w1 == 1 & wave == 3 & consent_individual == 1 & employer == 0 & ///
	status == 4 & used_w2_w1 == 0
replace left_firm_w2 = 0 if temp2 == 1
drop 	temp temp2

*Wave 3
	
gen 	left_firm_w3 = 1 if status == 3 & wave == 3 & employer==0
replace left_firm_w3 = 0 if status == 1 & wave == 3 & employer==0
replace left_firm_w3 = 0 if status == 4 & wave == 3 & employer==0
replace left_firm_w3 = . if wave == 3 & firm_att==1 & employer==0
replace left_firm_w3 = . if wave == 3 & inlist(id_indiv, 260175, 330633, 601383, 612011, 640326, 640328, 699933, 705895)

* ----------------------------------------------
* Outcome Variables: Quit and Layoff
* ----------------------------------------------

* ----------------------------------------------
* Quit Variables
* ----------------------------------------------

* Wave 2
gen     quit_w2_flag = 1 if left == 4 & wave == 2
bys     id_indiv: egen quit_w2_max = max(quit_w2_flag)
gen     quit_w2 = 1 if quit_w2_max == 1 & left_firm_w2 == 1
replace quit_w2 = 0 if left_firm_w2 == 0
replace quit_w2 =. if wave !=1
drop    quit_w2_flag quit_w2_max

* Wave 3
gen     quit_w3 = 1 if left == 4 & wave == 3
replace quit_w3 = 0 if left_firm_w3 == 0 & wave == 3
replace quit_w3 =. if wave !=3

* ----------------------------------------------
* Layoff Variables
* ----------------------------------------------

* Wave 2
gen     layoff_w2_flag = 1 if inlist(left, 1, 2) & wave == 2
bys     id_indiv: egen layoff_w2_max = max(layoff_w2_flag)
gen     layoff_w2 = 1 if layoff_w2_max == 1 & left_firm_w2 == 1
replace layoff_w2 = 0 if left_firm_w2 == 0
replace	layoff_w2 =. if wave !=1
drop    layoff_w2_flag layoff_w2_max

* Wave 3
gen     layoff_w3 = 1 if inlist(left, 1, 2) & wave == 3
replace layoff_w3 = 0 if left_firm_w3 == 0 & wave == 3
replace	layoff_w3 =. if wave !=3


	foreach var of varlist left_firm_w2 left_firm_w3 quit_w2 quit_w3 layoff_w2 layoff_w3  {
		
			eststo m`var'_`wave' : areg 	`var' ITT  left_firm_2021 missing_left_firm_2021    $se_indiv // $sample [pweight=w8] 
			
			qui summ `var' if ITT==0  
			local mu : di %5.2f r(mean)
			estadd local mu `mu'
	}
	

	esttab mleft_firm* mquit* mlayoff*,    keep(ITT) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
	nonumbers ///
    scalars("r2 R2" "mu Mean") mtitle("6 M" "18 M"  "6 M" "18 M"  "6 M" "18 M" ) mgroup("Left" "Quit" "Laid off" , pattern(1 0  1 0   1 0  1 0 ))
	
*Create Panel B ITT
esttab mleft_firm* mquit* mlayoff* using "$results\01_tables\Table_S1_robustness_nocontrols.tex", ///
   keep(ITT) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(mu N, fmt(%9.2f %9.0f) labels("Mean" "\(N\)")) ///		
    nomtitles ///
    noline  ///
	nonotes ///
	nonumbers /// 
	prehead("\multicolumn{13}{l}{\textbf{Panel C: Retention}}\\\hline\\[2pt]&\multicolumn{2}{c}{Left(0/1))}&\multicolumn{2}{c}{Quit (0/1)}&\multicolumn{2}{c}{Laid off (0/1)}&\multicolumn{3}{c}{}\\\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\\[2pt]") ///
		posthead("&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{6 M.}&\multicolumn{1}{c}{18 M.}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\\[2pt]\hline\\") ///
		prefoot("") ///
		postfoot("\hline\hline\\\end{tabular}") ///
		append	

